package sw6.xmlvalidator;

import java.io.File;
import sw6.xmlvalidator.definitions.*;
import sw6.xmlvalidator.updater.ProgramUpdater;

/**
 * Main file of this xml validation tool.
 * @author Lasse
 */
public class Main {

	/**
	 * Main entry of the tool.
	 * @param args Commandline arguments.
	 */
	public static void main(String args[]) {
		
		//Check if the tool should run in silent mode
		for(String arg : args) {
			if(arg.equals("--silent")) {
				//redirect std.out
				System.setOut(new java.io.PrintStream(new java.io.OutputStream(){ public void write(int b){}}));
			}
		}
		printAbout();

		//Check if a newer version of this tool exists
		double onlineVersion = ProgramUpdater.getLatestVersionNumber();

		//If there exists a newer version, write a note and kill this tool
		if(onlineVersion > Definitions.CURRENT_VERSION) {
			
			System.out.println("\tNotice: A newer version of this tool (version " + onlineVersion + ") is available at http://code.google.com/p/sw6android/.");
			System.out.println("\tPlease download the new version and use it to verify your settings.xml file.");
			System.exit(0);
		}

		//An argument is provided
		if(args.length > 0) {
			
			// Create new File instance
			File xmlFile = new File(args[0]);
			
			String fileTypeSplit[] = xmlFile.getName().split("\\.");
			String fileType = fileTypeSplit[fileTypeSplit.length-1];

			// Check if the file is of type xml
			if(fileType.equalsIgnoreCase("xml")) {
				
				//Check if the file exists
				if(xmlFile.exists()) {

					//Check locality of DTD
					if(DTDLocator.isDtdOnline(xmlFile, Definitions.URL_DTD) == 0) {
						System.out.println("\tWarning, you might not be using the latest DTD provided by sw6b. " +
								"\n\tPlease remember to use: " + Definitions.URL_DTD + " " +
								"\n\tbefore deploying your settings.xml file in your app.");
					}

					//Check structure and content of the XML file
					Settings settings = new Settings(xmlFile);
					
					//Handle different start-up issues
					if(settings.hasValidStructure()) {
						System.out.println("\nRESULT: Your XML-document is VALID :)");
						System.exit(Definitions.RETURN_CODE_VALID_XML);
					}
					else {
						System.out.println("\nRESULT: Your XML-document is INVALID.\n");
						System.exit(Definitions.RETURN_CODE_INVALID_XML);
					}
				}
				else {
					System.out.println("XML file does not exist. Check your argument.");
					printHelp();
				}
			} else {
				System.out.println("The provided file is not of type .xml");
			}
		}
		else {
			System.out.println("Insufficient arguments provided.");
			printHelp();
		}
	}

	private static void printHelp() {
		System.out.println("Help:");
		System.out.println("\tREQUIRED: Arg. 1: your setttings.xml file.");
		System.out.println("\tOPTIONAL: Arg. 2: \"--silent\". Will only output warnings and errors. The output will be directed to std.err.");
	}

	private static void printAbout() {
		System.out.println("************************************");
		System.out.println("sw6.admin settings.xml DTD-validator");
		System.out.println("sw6b@lcdev.dk | AAU SW | version " + Definitions.CURRENT_VERSION);
		System.out.println("************************************");
	}
}